<?php
namespace Kuoqi\DbDriver\Sql
{
use Kuoqi\Config;
class Page{
	public $Pager;
	public $PageCount;
	public $TotalCount;
	public $DefaultConfig;
	public $Buffer = '';
	public function create(){
		$this->validate();
		$this->pageNowBuild();
		$this->creatBianJie();
		$this->ShowStart();
		$this->ShowLast();
		$this->ShowNow();
		$this->ShowNext();
		$this->ShowEnd();
		return $this->Buffer;	
	}
	
	final function validate(){
		$this->DefaultConfig['pageShow'] = $this->Pager;
		$this->DefaultConfig['PageMax'] = ceil($this->TotalCount/$this->PageCount);
		$this->DefaultConfig['LineLeft'] = Config::get('DB_PAGE_LLINE');
		$this->DefaultConfig['LineRight'] = Config::get('DB_PAGE_RLINE');
		$this->DefaultConfig['LinePage'] = Config::get('DB_PAGE_LLINE')+Config::get('DB_PAGE_RLINE')+1;
		$this->DefaultConfig['Lang'] = Config::get('DB_PAGE_LANG');
		$this->DefaultConfig['pagekey'] = Config::get('DB_PAGE_VAR');
		$this->DefaultConfig['baseurl'] = Config::get('DB_PAGE_BSURL');
	}
	final function pageNowBuild(){
		if($this->DefaultConfig['pageShow']>=$this->DefaultConfig['PageMax'] && $this->DefaultConfig['PageMax']>1){
			$this->DefaultConfig['pageShow']=$this->DefaultConfig['PageMax'];
		}elseif($this->DefaultConfig['pageShow']<=1){
			$this->DefaultConfig['pageShow']=1;
		}
	}
	final function creatBianJie(){
		$this->DefaultConfig['LineStart'] = 1;
		$this->DefaultConfig['LineEnds'] = $this->DefaultConfig['PageMax'];
		if($this->DefaultConfig['pageShow']-$this->DefaultConfig['LineLeft']>$this->DefaultConfig['LineStart']){
			$this->DefaultConfig['LineStart'] = $this->DefaultConfig['pageShow']-$this->DefaultConfig['LineLeft'];
		}
		if($this->DefaultConfig['pageShow']+$this->DefaultConfig['LineRight']<$this->DefaultConfig['LineEnds']){
			$this->DefaultConfig['LineEnds'] = $this->DefaultConfig['pageShow']+$this->DefaultConfig['LineRight'];
		}
	}
	final function ShowStart(){
		if($this->DefaultConfig['Lang']=='en'){
			$tmp ="First";
		}else{
			$tmp ="&lt;&lt;";
		}
		$this->Buffer .="<li><a href='".$this->url($this->DefaultConfig['baseurl'],array($this->DefaultConfig['pagekey']=>1),false,true)."'>{$tmp}</a></li>";
		}
	final function ShowLast(){
		if($this->DefaultConfig['LinePage']-1>1){
			if($this->DefaultConfig['PageMax']<$this->DefaultConfig['LinePage']){
				$last=$this->DefaultConfig['PageMax'];
			}else{
				$last=$this->DefaultConfig['LinePage']-1;
			}
		}else{$last=1;}
		if($this->DefaultConfig['Lang']=='en'){$tmp ="Prev";}else{$tmp ="&lt;";}
		$this->Buffer .= "<li><a  href='".$this->url($this->DefaultConfig['baseurl'],array($this->DefaultConfig['pagekey']=>$last),false,true)."'>{$tmp}</a></li>";
		}
	final function ShowNow(){ 
		for($i=$this->DefaultConfig['LineStart'];$i<=$this->DefaultConfig['LineEnds'];$i++)
		{
			if($this->DefaultConfig['pageShow']==$i)
			{
				$this->Buffer .= "<li class='active'><a href='".$this->url($this->DefaultConfig['baseurl'],array($this->DefaultConfig['pagekey']=>$i),false,true)."'>".$i."</a></li>";
				}
				else
				{
					$this->Buffer .= "<li><a href='".$this->url($this->DefaultConfig['baseurl'],array($this->DefaultConfig['pagekey']=>$i),false,true)."'>".$i."</a></li>";
				}
			}
		}
	final function ShowNext(){
		if($this->DefaultConfig['LinePage']+1<$this->DefaultConfig['PageMax']){$next=$this->DefaultConfig['LinePage']+1;}else{$next=$this->DefaultConfig['PageMax'];}
		if($this->DefaultConfig['Lang']=='en'){$tmp ="Next";}else{$tmp ="&gt;";}
		$this->Buffer .= "<li><a  href='".$this->url($this->DefaultConfig['baseurl'],array($this->DefaultConfig['pagekey']=>$next),false,true)."'>{$tmp}</a></li>";
		}
	final function ShowEnd(){
		$this->Buffer .= "<li><a href='".$this->url($this->DefaultConfig['baseurl'],array($this->DefaultConfig['pagekey']=>$this->DefaultConfig['PageMax']),false,true)."'>";
		if($this->DefaultConfig['Lang']=='en'){
			$this->Buffer .="Last";
			}else{
				$this->Buffer .="&gt;&gt;";
				}
		$this->Buffer .="</a></li>";
	}
	final function url($baseurl,$params){
		$url = new \Kuoqi\Url($baseurl);
		//$url->setUrlbyRoute($baseurl,$params);
		$url->getInput();
		$url->addQuery($params);//增加属性信息
		return $url->execTolink();
	}
	
}
}